<?php

namespace App\Admin\Controllers;

use App\Models\Slide;
use App\Models\Activity;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Controllers\AdminController;

class SlidesController extends AdminController
{
    protected $title = '轮播管理';
    
    protected function grid()
    {
        $grid = new Grid(new Slide());
	
	    $grid->disableExport();
	    
	    $grid->column('position', '位置');
	    $grid->column('image', '图片')->image('', 50, 50);
	    $grid->column('is_shown', '显示')->bool();
	    $grid->column('created_at', '创建时间')->display(function () {
	    	return $this->created_at->toDatetimeString();
	    });
	    $grid->column('order', '排序')->editable();
        return $grid;
    }
    
    protected function detail($id)
    {
        $show = new Show(Slide::findOrFail($id));



        return $show;
    }
    
    protected function form()
    {
        $form = new Form(new Slide());
	    $form->select('position', '轮播位置')->options(config('site.slides.positions'))->rules('required');
	    $form->image('image', '轮播图片')->uniqueName()->removable()->rules('required');
	    
	    $form->radio('is_shown', '显示')->default(true)->options(config('site.bool'));
	    $form->number('order', '排序')->min(1)->default(1);
	    $form->select('type', '类型')->options(config('site.slides_type'))->when("activity", function (Form $form) { 

	         $form->select('activity_id','关联活动')->options(Activity::all()->pluck('title', 'id'));
	     });
	     $form->text('url', '跳转地址');

	   
        return $form;
    }
}
